﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using VideoStream.Interface;
using VideoStream.Model;

namespace VideoStream.Windows
{
    public partial class WindowsBase : Form, IWindowBase
    {

        #region Filds

        private bool showTitle = true;
        private Panel panel_Title;
        private double alphaSetp = 0.2d;
        protected int borderWidth = 1;
        protected Color borderColor = TVColor.Color_HighLight;
        protected bool showBorder = true;

        #endregion


        #region Property

        public bool ShowTitle
        {
            get { return showTitle; }
            set { showTitle = value; panel_Title.Visible = ShowTitle; panel_Title.BringToFront(); }
        }

        #endregion


        public WindowsBase()
        {
            InitializeComponent();

            InitTitle();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            if (showBorder)
            {
                Graphics g = e.Graphics;
                Pen p = new Pen(borderColor, borderWidth);
                g.DrawRectangle(p, 0, 0, this.Width - p.Width, this.Height - p.Width);
            }
        }

        public virtual void InitEvent()
        {
            // todo
            panel_Title.MouseDown += Panel_Title_MouseDown;
            panel_WTitle.MouseDown += Panel_Title_MouseDown;
            icon_Setting.MouseDown += Panel_Title_MouseDown;
            btn_Min.Click += (o, e) =>
            {
                this.WindowState = FormWindowState.Minimized;
            };
            btn_Close.Click += (o, e) =>
            {
                Close();
            };

            //// fade
            //Fade(0, 1);
        }

        public virtual void InitUI()
        {
            // todo

            this.StartPosition = FormStartPosition.CenterScreen;
            this.Text = "远程协作";
        }

        public virtual void UpdateUI()
        {
            // todo

            panel_Title.BackColor = Color.FromArgb(100, 30, 30, 30);
        }

        private void InitTitle()
        {
            panel_Title = new Panel();
            panel_Title.Dock = DockStyle.Top;
            panel_Title.Height = 32;
        }


        #region Control Event

        private void Panel_Title_MouseDown(object sender, MouseEventArgs e)
        {
            this.MoveWindows();
        }

        public void MoveWindows(object sendser, MouseEventArgs e)
        {
            this.MoveWindows();
        }

        #endregion


        #region Animation

        protected async void Fade(double start, double end)
        {
            this.Opacity = start;
            bool DO = (start - end) < 0;
            double setp = DO ? alphaSetp : alphaSetp * -1;
            while (this.Opacity != end)
            {
                this.Opacity += setp;
                await Task.Delay(17);
            }
        }

        #endregion


        //定义无边框窗体Form  
        [DllImport("user32.dll")]//*********************拖动无窗体的控件  
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);

        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;

        public void MoveWindows()
        {
            ReleaseCapture();
            SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
        }
    }
}
